package acceptTests;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import sessionManager.SessionManager;
import user.UserIF;
import user.blog.BlogIF;
import user.post.PostManager;

public class US06Facade {


	private SessionManager session;

	public US06Facade() {
		session = SessionManager.getInstance();

	}


	public void cleanPersistence() {
		session.cleanPersistence();
	}
	
	

	public String todaysDate() {
		SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
		String date = formatador.format(new Date());
		return date;
	}

	
	public void createProfile(String login, String senha, String nome_exibicao,
			String email, String sexo, String dataNasc, String endereco,
			String interesses, String quem_sou_eu, String filmes,
			String musicas, String livros) throws Exception {
		
		session.createUser(login, senha, nome_exibicao, email, sexo, dataNasc,
				endereco, interesses, quem_sou_eu, filmes, musicas, livros);
	}

	public String logon(String login, String password) throws Exception {
		String sessionId = session.loginUser(login, password);
		return sessionId;
	}

	public String createBlog(String idSession, String title, String description)
			throws Exception {
		
		return session.getUser(idSession).createBlog(idSession, title, description);
	}

	public String createPost(String idSession, String blogId, String title,
			String content) throws Exception {
		
		return session.getUser(idSession).getBlog(blogId).addPost(title, content);
	}

	public String attachSound(String sessionId, String postId,
			String description, String data) throws Exception {
		
		Iterator<BlogIF> itBlogs = session.getUser(sessionId).getBlogs().iterator();
		
		while(itBlogs.hasNext()) {
			BlogIF tmpBlog = itBlogs.next();
			if(tmpBlog.getPostByPID(postId) != null) {
				return tmpBlog.getPostByPID(postId).addAudio(description, data);
			}
		}
		
		return null;
	}

	public String attachMovie(String sessionId, String postId,
			String description, String data) throws Exception {
		
		Iterator<BlogIF> itBlogs = session.getUser(sessionId).getBlogs().iterator();
		
		while(itBlogs.hasNext()) {
			BlogIF tmpBlog = itBlogs.next();
			if(tmpBlog.getPostByPID(postId) != null) {
				return tmpBlog.getPostByPID(postId).addVideo(description, data);
			}
		}
		
		return null;
	}
	
	public String attachPicture(String sessionId, String postId,
			String description, String data) throws Exception {
		Iterator<BlogIF> itBlogs = session.getUser(sessionId).getBlogs().iterator();
		
		while(itBlogs.hasNext()) {
			BlogIF tmpBlog = itBlogs.next();
			if(tmpBlog.getPostByPID(postId) != null) {
				return tmpBlog.getPostByPID(postId).addImage(description, data);
			}
		}
		
		return null;
	}
	
	public String getPostInformation(String postId, String flag)
			throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getPostInformation(flag);
				}
			}
		}

		return null;
	}

	public int getNumberOfSounds(String postId) throws Exception {
		
		Iterator<UserIF> itUser = session.getUsers().iterator();
		
		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getNumberOfAudios();
				}
			}
		}
		
		return -1;
	}

	public int getNumberOfMovies(String postId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();
		
		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getNumberOfVideos();
				}
			}
		}
		
		return -1;
	}

	public int getNumberOfPictures(String postId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();
		
		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getNumberOfImages();
				}
			}
		}
		
		return -1;
	}

	public String getSound(String postId, int index) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getAudio(index).getId();
				}
			}
		}
		
		return null;
	}

	public String getSoundDescription(String audioId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				
				PostManager pm = tmpBlog.getPostManager();
				if(pm.getMedia(audioId) != null) {
					return pm.getMedia(audioId).getDescription();
				}
			}
		}
		
		return null;
	}

	public String getSoundData(String audioId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				
				PostManager pm = tmpBlog.getPostManager();
				if(pm.getMedia(audioId) != null) {
					return pm.getMedia(audioId).getData();
				}
			}
		}
		
		return null;
	}

	public String getMovieDescription(String videoId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				
				PostManager pm = tmpBlog.getPostManager();
				if(pm.getMedia(videoId) != null) {
					return pm.getMedia(videoId).getDescription();
				}
			}
		}
		
		return null;
	}

	public String getMovieData(String videoId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				
				PostManager pm = tmpBlog.getPostManager();
				if(pm.getMedia(videoId) != null) {
					return pm.getMedia(videoId).getData();
				}
			}
		}
		
		return null;
	}

	public String getMovie(String postId, int index) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getVideo(index).getId();
				}
			}
		}
		
		return null;
	}

	public String getPicture(String postId, int index) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getImage(index).getId();
				}
			}
		}
		
		return null;
	}

	public String getPictureDescription(String imageId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				
				PostManager pm = tmpBlog.getPostManager();
				if(pm.getMedia(imageId) != null) {
					return pm.getMedia(imageId).getDescription();
				}
			}
		}
		
		return null;
	}

	public String getPictureData(String imageId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				
				PostManager pm = tmpBlog.getPostManager();
				if(pm.getMedia(imageId) != null) {
					return pm.getMedia(imageId).getData();
				}
			}
		}
		
		return null;
	}

	public boolean logoff(String sessionId) throws Exception {
		return session.logoffUser(sessionId);

	}

	public void saveData() throws Exception {
		session.saveData();
	}

}